//
//  FindDetailViewController.m
//  Menu
//
//  Created by mc on 16/4/5.
//  Copyright © 2016年 xmj. All rights reserved.
//

#import "FindDetailViewController.h"
#import "UMSocial.h"
#import "MBProgressHUD.h"

@interface FindDetailViewController () <UMSocialUIDelegate, UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation FindDetailViewController

- (UIWebView*)webView{
    if (_webView == nil) {
        
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        _webView.delegate = self;
        _webView.userInteractionEnabled = YES;
    }
    return _webView;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"专题";
    
    
//    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAction) target:self action:@selector(shareMessage)];
    UIButton *shareButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 36)];
    [shareButton setBackgroundImage:[UIImage imageNamed:@"shareImg"] forState:UIControlStateNormal];
    [shareButton addTarget:self action:@selector(shareMessage) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
    self.navigationItem.rightBarButtonItem = rightButton;
    
    
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
    [self.view addSubview: self.webView];
    [self.webView loadRequest:request];
   // [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    
}

- (void)shareMessage{
    
//    [[UMSocialControllerService defaultControllerService] setShareText:@"分享内嵌文字" shareImage:[UIImage imageNamed:@"img_01"] socialUIDelegate:self];        //设置分享内容和回调对象
//    [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina].snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES);
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"570504a5e0f55a16b8001c03"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"img_01"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,nil]
                                       delegate:self  ];
}

-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的微博平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"分享成功！" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
        [alertView addAction:okAction];
        [self presentViewController:alertView animated:(YES) completion:^{
            
        }];
        
    }
}
//网页加载之前会调用此方法
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"%@",@"shouldStartLoadWithRequest");
    return YES;
}

//开始加载网页调用此方法
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"%@",@"webViewDidStartLoad");
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:webView animated:YES];
    hud.labelText = NSLocalizedString(@"Loading...", @"HUD loading title");

}
//网页加载完成调用此方法
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"%@",@"webViewDidFinishLoad");
    [MBProgressHUD hideHUDForView:webView animated:YES];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"%@",@"didFailLoadWithError");
    [MBProgressHUD hideHUDForView:self.webView animated:YES];
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"" message:[error localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil];
    //UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
    [alertView addAction:cancelAction];
    //[alertView addAction:okAction];
   // [alertView show];
    //alertControllerWithTitle
    [self presentViewController:alertView animated:(YES) completion:^{
        
    }];
}


@end
